include( 'cl_interface_create.lua' )
include( 'cl_interface_direct.lua' )
include( 'cl_interface_common.lua' )


if CLIENT then
local gi_Padding = 3
local gi_HotboxHeight = 200
local gi_PinButtonWidth = 40
gi_HotboxVerticalAnchor = 33

gi_ConstructButtonWidth =	(ScrW() - gi_PinButtonWidth) / 3
gi_DirectButtonWidth =	(ScrW() - gi_PinButtonWidth) / 3
gi_ProduceButtonWidth =	(ScrW() - gi_PinButtonWidth) / 3
go_HotboxFrame = nil
local go_HotboxCurrentPanel = nil

function CloseHotbox()
	go_HotboxFrame:Close()
	go_HotboxFrame = nil
end


local keyFramePanels = {}
local activeKeyFramePanel = nil

function SwitchKeyframePanel(panel, parent)
	
	if activeKeyFramePanel != panel and activeKeyFramePanel then
		activeKeyFramePanel.Panel:SetVisible(false)
		activeKeyFramePanel.Panel:SetEnabled(false)
	end

	activeKeyFramePanel = panel
	activeKeyFramePanel.Panel:SetParent(parent)
	activeKeyFramePanel.Panel:SetVisible(true)
	activeKeyFramePanel.Panel:SetEnabled(true)

end


local currentSelectedKeyframe = nil
local directKeyframes = {}
	directKeyframes["activeCameraTrack"] =  {{ParentKey = "activeCameraTrack", Percentage = 0.0, Panel = nil, Data = {CameraEntity = nil}}}
directKeyframes["blurEffectTrack"] = {{ParentKey = "blurEffectTrack", Percentage = 0.0, Panel = nil, Data = {BlendAmount = 0.0, MotionBlurTime = 0.0}}}
directKeyframes["colortintEffectTrack"] = {{ParentKey = "colortintEffectTrack", Percentage = 0.0, Panel = nil, Data = {Brightness = 0.0, Contrast = 1.0, Red = 0.0, Green = 0.0, Blue = 0.0}}}
directKeyframes["glowEffectTrack"] = {{ParentKey = "glowEffectTrack", Percentage = 0.0, Panel = nil, Data = {Multiply = 0.0, Spread = 0.5, Color = 0.5, Red = 0.5, Green = 0.5, Blue = 0.5 }}}

function activeCameraTrackKeyframePanel(parent, keyframeModel)

	keyFramePanels["activeCameraTrack"] = keyFramePanels["activeCameraTrack"] or {}	
	if (keyFramePanels["activeCameraTrack"].Panel == nil) then

		local panel = GetPanelEx(0,gi_HotboxVerticalAnchor,parent:GetWide(), parent:GetTall(), PANEL_LIGHT, parent)
		panel:SetPos(5,40)
		panel:SetSize(parent:GetWide() - 10, parent:GetTall() - (40 + 5))
			
		local activeCameraTrack = GetTextLabelEx("Active Camera Properties", "Default", panel)	
		activeCameraTrack:SetPos(5,5)

		local sceneCameras = GetTextLabel("Scene Camera Tracks:", panel)	
		sceneCameras:SetPos(5,30)
		local dropList = GetDropMenu({}, panel)
		dropList:SetPos(5,45)
		dropList:SetSize(panel:GetWide() - 10, 20)
		keyFramePanels["activeCameraTrack"].Panel = panel
		keyFramePanels["activeCameraTrack"].CamerasDropList = dropList
	end

	local dropListItems = {}
	local sceneEntities = ents.GetAll()
	for _, sceneEntity in pairs(sceneEntities) do

		if sceneEntity:GetClass() == "animated_camera" then

			//We need to know if this camera is the first in it's track, if so
			//then add it to the list 
			//TODO:just call GetName() directily??
			if GetRootNode(sceneEntity) == sceneEntity then
				table.insert(dropListItems, {Label = sceneEntity.BaseClass.GetName(sceneEntity), Func = function() keyframeModel.Data.CameraEntity = sceneEntity end})
			end
		end
	end
	print(keyframeModel.Data.CameraEntity)
	local cameraEntity = keyframeModel.Data.CameraEntity
	local cameraLabel = "Not Set"
	if cameraEntity and cameraEntity:IsValid() then
		cameraLabel = cameraEntity.BaseClass.GetName(cameraEntity)
	end 
	keyFramePanels["activeCameraTrack"].CamerasDropList:SetItems(dropListItems)
	keyFramePanels["activeCameraTrack"].CamerasDropList:SetText(cameraLabel) 
	return keyFramePanels["activeCameraTrack"].Panel
end

function BlurKeyframePanel(parent, keyframeModel)

	keyFramePanels["blur"] = keyFramePanels["blur"] or {}	
	if (keyFramePanels["blur"].Panel == nil) then

		local panel = GetPanelEx(0,gi_HotboxVerticalAnchor,parent:GetWide(), parent:GetTall(), PANEL_LIGHT, parent)
		panel:SetPos(5,40)
		panel:SetSize(parent:GetWide() - 10, parent:GetTall() - (40 + 5))
			
		local motionBlurLabel = GetTextLabelEx("Motion Blur Properties:", "Default", panel)	
		motionBlurLabel:SetPos(5,5)

		local motionBlurTime = GetNumberSlider("Duration", 0, 1, 0, nil, panel)
		motionBlurTime:SetPos(5, 25)
		motionBlurTime:SetSize(parent:GetWide() - 10, 30)

		local blendAmount = GetNumberSlider("Blend Amount", 0, 1, 0, nil, panel)
		blendAmount:SetPos(5, 65)
		blendAmount:SetSize(parent:GetWide() - 10, 30)

		keyFramePanels["blur"].Panel = panel
		keyFramePanels["blur"].MotionBlurTime = motionBlurTime
		keyFramePanels["blur"].BlendAmount = blendAmount
		
	end

	if keyframeModel then
		local Data = keyframeModel.Data
		keyFramePanels["blur"].BlendAmount.OnValueChanged = function(panel, value) Data.BlendAmount = value end
		keyFramePanels["blur"].BlendAmount:SetValue(Data.BlendAmount)
		keyFramePanels["blur"].MotionBlurTime.OnValueChanged = function(panel, value) Data.MotionBlurTime = value end
		keyFramePanels["blur"].MotionBlurTime:SetValue(Data.MotionBlurTime)
	end

	return keyFramePanels["blur"].Panel
end

function ColorTintKeyframePanel(parent, keyframeModel)

	keyFramePanels["colorcontrol"] = keyFramePanels["colorcontrol"] or {}	
	if (keyFramePanels["colorcontrol"].Panel == nil) then

		local panel = GetPanelEx(0,gi_HotboxVerticalAnchor,parent:GetWide(), parent:GetTall(), PANEL_LIGHT, parent)
		panel:SetPos(5,40)
		panel:SetSize(parent:GetWide() - 10, parent:GetTall() - (40 + 5))
			
		local motionBlurLabel = GetTextLabelEx("Color Control:", "Default", panel)	
		motionBlurLabel:SetPos(5,5)

		local contrast = GetNumberSlider("Contrast", 0, 1, 0, 
		function(panel, value) 
			keyframeModel.Data.Constrast = value 
		end, panel)
		contrast:SetPos(5, 25)
		contrast:SetSize(parent:GetWide() / 2 - 10, 30)

 		local brightness = GetNumberSlider("Brightness", -1, 1, 0, nil, panel)
		brightness:SetPos(5 , 65)
		brightness:SetSize(parent:GetWide() / 2 - 10, 30)

		GetSeperator(parent:GetWide() / 2,25, 0, parent:GetTall() - 30, panel)

		local redness = GetNumberSlider("Red", 0, 255, 0, nil, panel)
		redness:SetPos(parent:GetWide() / 2 + 10, 25)
		redness:SetSize(parent:GetWide() / 3 + 10, 30)

		local greenness = GetNumberSlider("Green", 0,255, 0, nil, panel)
		greenness:SetPos(parent:GetWide() / 2 + 10, 55)
		greenness:SetSize(parent:GetWide() / 3 + 10, 30)

		local blueness = GetNumberSlider("Blue", 0, 255, 0, nil, panel)
		blueness:SetPos(parent:GetWide() / 2 + 10, 85)
		blueness:SetSize(parent:GetWide() / 3 + 10, 30)

		keyFramePanels["colorcontrol"].Panel = panel
		keyFramePanels["colorcontrol"].Contrast = contrast
		keyFramePanels["colorcontrol"].Brightness = brightness
		keyFramePanels["colorcontrol"].Redness = redness
		keyFramePanels["colorcontrol"].Greeness = greenness
		keyFramePanels["colorcontrol"].Blueness = blueness
	end

	if keyframeModel then
		local data = keyframeModel.Data
		keyFramePanels["colorcontrol"].Contrast.OnValueChanged = function(panel, value) keyframeModel.Data.Contrast = value end
		keyFramePanels["colorcontrol"].Contrast:SetValue(data.Contrast)
		keyFramePanels["colorcontrol"].Brightness.OnValueChanged = function(panel, value) keyframeModel.Data.Brightness = value end
		keyFramePanels["colorcontrol"].Brightness:SetValue(data.Brightness)
		keyFramePanels["colorcontrol"].Redness.OnValueChanged = function(panel, value) keyframeModel.Data.Red = value end
		keyFramePanels["colorcontrol"].Redness:SetValue(data.Red)
		keyFramePanels["colorcontrol"].Greeness.OnValueChanged = function(panel, value) keyframeModel.Data.Green = value end
		keyFramePanels["colorcontrol"].Greeness:SetValue(data.Green)
		keyFramePanels["colorcontrol"].Blueness.OnValueChanged = function(panel, value) keyframeModel.Data.Blue = value end
		keyFramePanels["colorcontrol"].Blueness:SetValue(data.Blue)
	end

	return keyFramePanels["colorcontrol"].Panel
end

function GlowControlKeyframePanel(parent, keyframeModel)

	keyFramePanels["glowcontrol"] = keyFramePanels["glowcontrol"] or {}
	if (keyFramePanels["glowcontrol"].Panel == nil) then

		local panel = GetPanelEx(0,gi_HotboxVerticalAnchor,parent:GetWide(), parent:GetTall(), PANEL_LIGHT, parent)
		panel:SetPos(5,40)
		panel:SetSize(parent:GetWide() - 10, parent:GetTall() - (40 + 5))
			
		local motionBlurLabel = GetTextLabelEx("Glow Control:", "Default", panel)	
		motionBlurLabel:SetPos(5,5)

		local amout = GetNumberSlider("Amount", 0, 1, 0, nil, panel)
		amout:SetPos(5, 25)
		amout:SetSize(parent:GetWide() / 1 - 10, 30)

		local spread = GetNumberSlider("Spread", 0, 10, 0, nil, panel)
		spread:SetPos(5, 55)
		spread:SetSize(parent:GetWide() / 1 - 10, 30)

		local color = GetNumberSlider("Color", 0, 10, 0, nil, panel)
		color:SetPos(5, 85)
		color:SetSize(parent:GetWide() / 1 - 10, 30)

		keyFramePanels["glowcontrol"].Panel = panel
		keyFramePanels["glowcontrol"].Amount = amout
		keyFramePanels["glowcontrol"].Spread = spread
		keyFramePanels["glowcontrol"].Color = color
	end

	if keyframeModel then
		local Data = keyframeModel.Data

		keyFramePanels["glowcontrol"].Amount.OnValueChanged = function(panel, value) keyframeModel.Data.Multiply = value  end
		keyFramePanels["glowcontrol"].Amount:SetValue(Data.Multiply)
		keyFramePanels["glowcontrol"].Spread.OnValueChanged = function(panel, value) keyframeModel.Data.Spread = value end
		keyFramePanels["glowcontrol"].Spread:SetValue(Data.Spread)
		keyFramePanels["glowcontrol"].Color.OnValueChanged = function(panel, value) keyframeModel.Data.Color = value end
		keyFramePanels["glowcontrol"].Color:SetValue(Data.Color)
	end

	return keyFramePanels["glowcontrol"].Panel
end

function NoKeyframeSelectedPanel(parent)

	keyFramePanels["nokfselected"] = keyFramePanels["nokfselected"] or {}

	if (keyFramePanels["nokfselected"].Panel == nil) then

		local panel = GetPanelEx(0,gi_HotboxVerticalAnchor,parent:GetWide(), parent:GetTall(), PANEL_LIGHT, parent)
		panel:SetPos(5,40)
		panel:SetSize(parent:GetWide() - 10, parent:GetTall() - (40 + 5))

		local mainmsg = GetTextLabelEx("No key frame selected", "Default", panel)	
		mainmsg:SetPos(25,15)
			
		local message = GetTextLabel("Left click a key frame to manipluate\nits position. Right click to access it's\nproperties and parameters", panel)	
		message:SetPos(15,45)

		keyFramePanels["nokfselected"].Panel = panel
		keyFramePanels["nokfselected"].MotionBlurAmout = 0
	end
	return keyFramePanels["nokfselected"]
end

tab = {} 
tab[ "$pp_colour_addr" ] = 0
tab[ "$pp_colour_addg" ] = 0 
tab[ "$pp_colour_addb" ] = 0
tab[ "$pp_colour_brightness" ] = 0 
tab[ "$pp_colour_contrast" ] = 1
tab[ "$pp_colour_colour" ] = 1
tab[ "$pp_colour_mulr" ] = 0 
tab[ "$pp_colour_mulg" ] = 0 
tab[ "$pp_colour_mulb" ] = 0  

blur = {}
blur["blend"] = 0
blur["time"] = 0

glow = {}
glow["multiply"] = 0
glow["spread"] = 0
glow["color"] = 0
 local function RenderSpecialEffects() 
if gAllowScreenEffects == true then
	DrawColorModify( tab ) 
	DrawMotionBlur( 0.5, blur["blend"], blur["time"]) 
	DrawBloom( 0.0, glow["multiply"], glow["spread"], glow["spread"], 5, glow["color"], glow["red"], glow["green"], glow["blue"])

	DrawBloom( 0.25, glow["multiply"], 
	glow["spread"], 
	glow["spread"], 
	5, glow["color"], 
	1, 1, 1)
end
 end 
 hook.Add( "RenderScreenspaceEffects", "RenderSpecialEffects", RenderSpecialEffects)  

function DirectPanel(parent)

	local panel = GetPanel(0,gi_HotboxVerticalAnchor,parent:GetWide(), parent:GetTall(), parent)
	
	local firstPanel = GetPanelEx(gi_Padding * 2,gi_Padding, 100,
	panel:GetTall() - 40, PANEL_LIGHT, panel) 

	local timeLabel = GetTextLabelEx("Tracks", "Default", firstPanel)	
	timeLabel:SetPos(20,5)


	GetSeperator(10,35, 80,0, firstPanel)

	local activeCameraTrack = GetTextLabel("Active Camera", firstPanel)	
	activeCameraTrack:SetPos(20,42.5)

	GetSeperator(10,60, 80,0, firstPanel)
	local liveProcessing = GetTextLabel("Special Effects", firstPanel)	
	liveProcessing:SetPos(20,67.5)

	local blurProcess = GetTextLabel("Motion Blur", firstPanel)	
	blurProcess:SetPos(30,84)

	local colortintProcess = GetTextLabel("Color Control", firstPanel)	
	colortintProcess:SetPos(22,102)

	local colortintProcess = GetTextLabel("Glow", firstPanel)	
	colortintProcess:SetPos(55,120)

					 
	local ticks = 180
	local secondPanel = GetPanelEx(gi_Padding * 3 + 100,gi_Padding, ScrW() - (gi_Padding * 4 + 150 + 150),panel:GetTall() - 40, PANEL_LIGHT, panel) 
 	local activeCameraTrack = GetKeyFrameBar(secondPanel, directKeyframes["activeCameraTrack"], 0, ticks)
	local blurEffectTrack = GetKeyFrameBar(secondPanel, directKeyframes["blurEffectTrack"], 0, ticks)
	local colortintEffectTrack = GetKeyFrameBar(secondPanel, directKeyframes["colortintEffectTrack"], 0, ticks)
	local glowEffectTrack = GetKeyFrameBar(secondPanel, directKeyframes["glowEffectTrack"], 0, ticks)

	local lastActiveCamera = nil

  	local rwndButton = GetImageButton("gui/MMMatRwndButton", 26, 26, 
	function()
		gOverridePlayerMove = true
		activeCameraTrack:SetState(MMDKeyFrameBar.PAUSED)
		activeCameraTrack:DoRewind()
		blurEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		blurEffectTrack:DoRewind()		
		colortintEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		colortintEffectTrack:DoRewind()		
		glowEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		glowEffectTrack:DoRewind()
	end, secondPanel)
	rwndButton:SetPos( 18, secondPanel:GetTall() - 26) 
 
  	local playButton = GetImageButton("gui/MMMatPlayButton", 26, 26, 
	function()
		gOverridePlayerMove	= true
		activeCameraTrack:SetState(MMDKeyFrameBar.PLAYING)
		blurEffectTrack:SetState(MMDKeyFrameBar.PLAYING)
		colortintEffectTrack:SetState(MMDKeyFrameBar.PLAYING)
		glowEffectTrack:SetState(MMDKeyFrameBar.PLAYING)
	end, secondPanel)
	playButton:SetPos( 46, secondPanel:GetTall() - 26) 
				 
  	local stopButton = GetImageButton("gui/MMMatStopButton", 26, 26, 
	function()
lastActiveCamera=nil
	tab[ "$pp_colour_addr" ] = 0
	tab[ "$pp_colour_addg" ] = 0 
	tab[ "$pp_colour_addb" ] = 0
	tab[ "$pp_colour_brightness" ] = 0 
	tab[ "$pp_colour_contrast" ] = 1
	tab[ "$pp_colour_colour" ] = 1
	tab[ "$pp_colour_mulr" ] = 0 
	tab[ "$pp_colour_mulg" ] = 0 
	tab[ "$pp_colour_mulb" ] = 0  

	blur["blend"] = 0
	blur["time"] = 0

	glow["multiply"] = 0
	glow["spread"] = 0
	glow["color"] = 0

		gOverridePlayerMove = false
		activeCameraTrack:SetState(MMDKeyFrameBar.STOPPED)
		blurEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
		colortintEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
		glowEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
	end, secondPanel)
	stopButton:SetPos( 72, secondPanel:GetTall() - 26) 
				 
  	local pauseButton = GetImageButton("gui/MMMatPauseButton", 26, 26, 
	function()
		gOverridePlayerMove = true
		activeCameraTrack:SetState(MMDKeyFrameBar.PAUSED)
		blurEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		colortintEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		glowEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
	end, secondPanel)
	pauseButton:SetPos( 100, secondPanel:GetTall() - 26) 

  	local ffwdButton = GetImageButton("gui/MMMatFFwdButton", 26, 26, 
	function()
		gOverridePlayerMove = true
		activeCameraTrack:SetState(MMDKeyFrameBar.PAUSED)
		activeCameraTrack:DoFastForward()
		blurEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		blurEffectTrack:DoFastForward()		
		colortintEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		colortintEffectTrack:DoFastForward()
		glowEffectTrack:SetState(MMDKeyFrameBar.PAUSED)
		glowEffectTrack:DoFastForward()
	end, secondPanel)
	ffwdButton:SetPos( 128, secondPanel:GetTall() - 26) 
	

	GetSeperator(160,secondPanel:GetTall() - 24, 0,24, secondPanel)

	local wide = secondPanel:GetWide() - 60

	local timeLabel = GetTextLabelEx("    [0: 0: 0.0000]    ", "Trebuchet22", secondPanel)	
	timeLabel:SizeToContents()
	timeLabel:SetPos(170,secondPanel:GetTall() - timeLabel:GetTall() - 4)
	timeLabel.Think = function()
		
		local minutes, secondsFrac = math.modf(activeCameraTrack.CurrentTime / 60)
		local hours, minutesFrac = math.modf(minutes / 60.0)

		timeLabel:SetText(string.format("[%2i:%2i:%2.2f]", hours, minutes, secondsFrac * 60.0))

	end
	
	local start = 15

	local timelineTicks = GetPanelEx(gi_Padding,gi_Padding, secondPanel:GetWide() - gi_Padding * 2,
	30, Color(0,255,0), secondPanel) 
	timelineTicks.Paint = function()
	 

		local step = wide / (ticks)

		surface.SetDrawColor(255,255,255,255)	
		surface.DrawLine( start + step, 22,start + secondPanel:GetWide() - 60 + 1, 22)

		for i=1,(ticks) do 
		
		if (i % 60) == 0 or i == 1 then
				
			surface.SetDrawColor( 233, 233, 233, 255 )
			surface.SetTextColor( 233, 233, 233, 255 )
			surface.SetTextPos( start + (i * step - 10), 0)
surface.SetFont("Default")
			surface.DrawText( string.format("%i:00", i / 60))

			surface.DrawLine( start + (i * step), 16,start + (i * step), 22) 
		else
			if i % 2 == 0 then
			surface.SetDrawColor( 180, 180, 180, 255 )
			surface.DrawLine( start + (i * step), 17,start + (i * step), 20) 
			end
		end

		end
	end

	local addButtonWidth = 40


	activeCameraTrack:SetPos(start * 1.25,40)
	activeCameraTrack:SetSize(wide + activeCameraTrack.AddButton:GetWide(),20)

	blurEffectTrack:SetPos(start * 1.25,82)
	blurEffectTrack:SetSize(wide+ blurEffectTrack.AddButton:GetWide(),15)

	colortintEffectTrack:SetPos(start * 1.25,100)
	colortintEffectTrack:SetSize(wide+ colortintEffectTrack.AddButton:GetWide(),15)

	glowEffectTrack:SetPos(start * 1.25,118)
	glowEffectTrack:SetSize(wide+ glowEffectTrack.AddButton:GetWide(),15)			   

	local thirdPanel = GetPanelEx(secondPanel:GetWide() + gi_Padding * 4 + 100,gi_Padding, 
	ScrW() - (secondPanel:GetWide() + (gi_Padding * 5 + 100)),panel:GetTall() - 40, PANEL_LIGHT, panel) 

	local keyframeProperties = GetTextLabelEx("Keyframe Control", "Default", thirdPanel)	
	keyframeProperties:SetPos( thirdPanel:GetWide() / 2 - keyframeProperties:GetWide() / 2,5)
	GetSeperator(10,35, thirdPanel:GetWide() - 20,0, thirdPanel)


  	local removeKeyframeButton = GetImageButton("gui/MMMatTrash", 26, 26, nil, thirdPanel)
	removeKeyframeButton.DoClick = function() 
		if currentSelectedKeyframe then

			local newKeys = {}
			for k,o in pairs(directKeyframes[currentSelectedKeyframe.ParentKey]) do
				if o != currentSelectedKeyframe then
					table.insert(newKeys, o)
				end
			end
			directKeyframes[currentSelectedKeyframe.ParentKey] = newKeys

			//TODO: check parent is of keyframebar type
			local keyframeBar = currentSelectedKeyframe.Panel:GetParent()
			keyframeBar:RemoveKeyframe(currentSelectedKeyframe)
			currentSelectedKeyframe = nil
		end

		removeKeyframeButton:SetDisabled(true)
		SwitchKeyframePanel(NoKeyframeSelectedPanel(thirdPanel),thirdPanel)
	end

	removeKeyframeButton:SetPos(thirdPanel:GetWide() - (removeKeyframeButton:GetWide() + 10), 5)


	local OnClickKeyframe = function (keyframeModel)
		if keyframeModel != currentSelectedKeyframe then

			if currentSelectedKeyframe then
				currentSelectedKeyframe.Panel.KeyColor = KEY_FRAME_IDLE
			end
			removeKeyframeButton:SetDisabled(false)
			currentSelectedKeyframe = keyframeModel
			return true
		else
			currentSelectedKeyframe.Panel.KeyColor = KEY_FRAME_IDLE
			SwitchKeyframePanel(NoKeyframeSelectedPanel(thirdPanel),thirdPanel)
			removeKeyframeButton:SetDisabled(true)
			currentSelectedKeyframe = nil
			return false
		end
	end


	activeCameraTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		activeCameraTrack:SetState(MMDKeyFrameBar.STOPPED)
	end
	blurEffectTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		blurEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
	end
	colortintEffectTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		colortintEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
	end
	glowEffectTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		glowEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
	end


	activeCameraTrack.OnSample = function(frac, keyDataA, keyDataB)

		local entityA = keyDataA.Data.CameraEntity

		if entityA and entityA:IsValid() then
			
			gViewData = entityA:PlaybackData()
			if entityA != lastActiveCamera then
				//causes playback to start from begininng
				entityA.onPlaybackNode = nil	
				entityA.onPlaybackNodeTime = 0
				lastActiveCamera =entityA
			end
		end
	end
	activeCameraTrack.OnAddKeyframe = function() 
		local newModel = {ParentKey = "activeCameraTrack", Percentage = 0.0, Panel = nil, Data = {CameraEntity = nil}}
		table.insert(directKeyframes["activeCameraTrack"], 1, newModel)
		return newModel
	end
	activeCameraTrack.OnLeftClickKeyframe = function(keyframeModel) 
		if OnClickKeyframe(keyframeModel) then
			SwitchKeyframePanel(activeCameraTrackKeyframePanel(thirdPanel, keyframeModel),thirdPanel) 
		end
	end
	
	blurEffectTrack.OnSample = function(frac, keyDataA, keyDataB)
	
		local minPerc = keyDataA.Percentage
		local minData = keyDataA.Data
		local maxPerc = keyDataB.Percentage
		local maxData = keyDataB.Data

		local percDelta = maxPerc - minPerc
		if percDelta > 0.0 then
			local lerp = (frac - minPerc) / percDelta
			local invLerp = 1.0 - lerp

			blur[ "blend" ] = (invLerp * minData.BlendAmount) + (lerp * maxData.BlendAmount)
			blur[ "time" ] = (invLerp * minData.MotionBlurTime) + (lerp * maxData.MotionBlurTime)
		else
			blur[ "blend" ] = minData.BlendAmount
			blur[ "time" ] = minData.MotionBlurTime
		end
	end
	blurEffectTrack.OnAddKeyframe = function() 
	local newModel = {ParentKey = "blurEffectTrack", Percentage = 0.0, Panel = nil, Data = {BlendAmount = 0.5, MotionBlurTime = 0.5}}
		table.insert(directKeyframes["blurEffectTrack"], 1, newModel)
		return newModel
	end
	blurEffectTrack.OnLeftClickKeyframe = function(keyframeModel) 
		if OnClickKeyframe(keyframeModel) then
			SwitchKeyframePanel(BlurKeyframePanel(thirdPanel, keyframeModel),thirdPanel) 
		end
	end

	colortintEffectTrack.OnSample = function(frac, keyDataA, keyDataB)
	
		local minPerc = keyDataA.Percentage
		local minData = keyDataA.Data
		local maxPerc = keyDataB.Percentage
		local maxData = keyDataB.Data

		local percDelta = maxPerc - minPerc
		if percDelta > 0.0 then
		
			local lerp = (frac - minPerc) / percDelta
			local invLerp = 1.0 - lerp

			tab[ "$pp_colour_addr" ] = 0
			tab[ "$pp_colour_addg" ] = 0 
			tab[ "$pp_colour_addb" ] = 0 
			tab[ "$pp_colour_brightness" ] = (invLerp * minData.Brightness) + (lerp * maxData.Brightness)
			tab[ "$pp_colour_contrast" ] = (invLerp * minData.Contrast) + (lerp * maxData.Contrast)
			tab[ "$pp_colour_colour" ] = 1 
			tab[ "$pp_colour_mulr" ] = (invLerp * minData.Red) + (lerp * maxData.Red)
			tab[ "$pp_colour_mulg" ] = (invLerp * minData.Green) + (lerp * maxData.Green)
			tab[ "$pp_colour_mulb" ] = (invLerp * minData.Blue) + (lerp * maxData.Blue)

		else
			
			tab[ "$pp_colour_addr" ] = 0
			tab[ "$pp_colour_addg" ] = 0 
			tab[ "$pp_colour_addb" ] = 0 
			tab[ "$pp_colour_brightness" ] = minData.Brightness
			tab[ "$pp_colour_contrast" ] = minData.Contrast
			tab[ "$pp_colour_colour" ] = 1 
			tab[ "$pp_colour_mulr" ] = minData.Red
			tab[ "$pp_colour_mulg" ] = minData.Green
			tab[ "$pp_colour_mulb" ] = minData.Blue

		end
	end

	colortintEffectTrack.OnAddKeyframe = function() 
		local newModel = {ParentKey = "colortintEffectTrack", Percentage = 0.0, Panel = nil, Data = {Brightness = 0.5, Contrast = 0.5, Red = 0.1, Green = 0.1, Blue = 0.1}}
		table.insert(directKeyframes["colortintEffectTrack"], 1, newModel)
		return newModel
	end
	colortintEffectTrack.OnLeftClickKeyframe = function(keyframeModel) 
		if OnClickKeyframe(keyframeModel) then
			SwitchKeyframePanel(ColorTintKeyframePanel(thirdPanel, keyframeModel),thirdPanel) 
		end
	 end
	
	glowEffectTrack.OnSample = function(frac, keyDataA, keyDataB)
	
		local minPerc = keyDataA.Percentage
		local minData = keyDataA.Data
		local maxPerc = keyDataB.Percentage
		local maxData = keyDataB.Data

		local percDelta = maxPerc - minPerc
		if percDelta > 0.0 then
			local lerp = (frac - minPerc) / percDelta
			local invLerp = 1.0 - lerp

			glow[ "multiply" ] = (invLerp * minData.Multiply) + (lerp * maxData.Multiply)
			glow[ "spread" ] = (invLerp * minData.Spread) + (lerp * maxData.Spread)
			glow[ "color" ] = (invLerp * minData.Color) + (lerp * maxData.Color)
		else
			glow[ "multiply" ] = minData.Multiply
			glow[ "spread" ] = minData.Spread
			glow[ "color" ] = minData.Color
		end
	end
	glowEffectTrack.OnAddKeyframe = function() 
		local newModel = {ParentKey = "glowEffectTrack",Percentage = 0.0, Panel = nil, Data = {Multiply = 0.5, Spread = 0.5, Color = 0.5, Red = 0.5, Green = 0.5, Blue = 0.5 }}
		table.insert(directKeyframes["glowEffectTrack"], 1, newModel)
		return newModel
	end
	glowEffectTrack.OnLeftClickKeyframe = function(keyframeModel) 
		if OnClickKeyframe(keyframeModel) then
			SwitchKeyframePanel(GlowControlKeyframePanel(thirdPanel, keyframeModel),thirdPanel) 
		end
	end

	if currentSelectedKeyframe == nil then
		removeKeyframeButton:SetDisabled(true)
	end

	SwitchKeyframePanel(NoKeyframeSelectedPanel(thirdPanel),thirdPanel)
end

local startPercent = 0.0
local endPercent = 100.0
local isRecording = false
local recordTime = 0.0 
function ProducePanel(parent)
	
	local panel = GetPanel(0,gi_HotboxVerticalAnchor,parent:GetWide(), parent:GetTall(), parent)
	
	local firstPanel = GetPanelEx(gi_Padding * 2,gi_Padding, 180,
	panel:GetTall() - 40, PANEL_LIGHT, panel) 

	local displayLabel = GetTextLabelEx("Recording", "Default", firstPanel)	
	displayLabel:SetPos(20,5)

	GetSeperator(10,5 + displayLabel:GetTall() + (gi_Padding * 2), 160,0, firstPanel)	

	//local sourceLabel = GetTextLabel("Range", firstPanel)	
	//sourceLabel:SetPos(10,5 + displayLabel:GetTall() + (gi_Padding * 4))


	//local startSlider = GetNumberSlider("%Start", 0, 100, startPercent, nil, panel)
	//startSlider:SetPos(10, displayLabel:GetTall() + (gi_Padding * 8) + sourceLabel:GetTall())
	//startSlider:SetSize((firstPanel:GetWide() / 2) - 10, 30)

	//local endSlider = GetNumberSlider("%End", 0, endPercent, 100, nil, panel)
	//endSlider:SetPos(10 + (firstPanel:GetWide() / 2), displayLabel:GetTall() + (gi_Padding * 8) + sourceLabel:GetTall())
	//endSlider:SetSize((firstPanel:GetWide() / 2) - 10, 30)

	//startSlider.Think = function()
	//	if startSlider.Value > endPercent then
	//		startSlider.Value = endPercent
	//	end
	//	startPercent = startSlider.Value
	//end

	//endSlider.Think = function()
	//	if endSlider.Value < startPercent then
	//		endSlider.Value = startPercent
	//	end
	//	endPercent = endSlider.Value
	//end

	
	
	local timelimitLabel = GetTextLabel("Time Limit", firstPanel)	
	timelimitLabel:SetPos(10,5 + displayLabel:GetTall() + (gi_Padding * 2) + timelimitLabel:GetTall())

	local timeLimitSlider = GetNumberSlider("Seconds", 0, 180, endPercent, nil, panel)
	timeLimitSlider:SetPos(10, 5 + displayLabel:GetTall() + (gi_Padding * 8) + timelimitLabel:GetTall())
	timeLimitSlider:SetSize(firstPanel:GetWide() - 10, 30)
	timeLimitSlider.Think = function()
		endPercent = timeLimitSlider.Value
	end

	local timelimitNote = GetTextLabel("Note: setting the time limit to 0.0 will\ncause recording to continue until stopped", firstPanel)	
timelimitNote:SetPos(10,5 + timeLimitSlider:GetTall() + displayLabel:GetTall() + (gi_Padding * 12) + timelimitLabel:GetTall())

	local secondPanel = GetPanelEx(gi_Padding * 3 + 180,gi_Padding, 180,panel:GetTall() - 40, PANEL_LIGHT, panel) 
 	local controlLabel = GetTextLabelEx("Control", "Default", secondPanel)	
	controlLabel:SetPos(20,5)
 	
	GetSeperator(10,5 + controlLabel:GetTall() + (gi_Padding * 2), 160,0, secondPanel)	

	local notecontrolLabel = GetTextLabel("Note: The space bar key can also\nbe pressed while recording to cancel", secondPanel)	
	notecontrolLabel:SetPos(20,controlLabel:GetTall() + 5 + (gi_Padding * 4))

  	local recordButton = GetImageButton("gui/MMMatRecordButton", 75, 75, function() end, secondPanel)
	local ticks = 180
	local thirdPanel = GetPanelEx(gi_Padding * 5 + 360,gi_Padding, panel:GetWide() - (firstPanel:GetWide() + 20+ secondPanel:GetWide()),panel:GetTall() - 40, PANEL_LIGHT, panel) 
 	local activeCameraTrack = GetKeyFrameBarEx(thirdPanel, directKeyframes["activeCameraTrack"], 0, ticks, false)
	local blurEffectTrack = GetKeyFrameBarEx(thirdPanel, directKeyframes["blurEffectTrack"], 0, ticks, false)
	local colortintEffectTrack = GetKeyFrameBarEx(thirdPanel, directKeyframes["colortintEffectTrack"], 0, ticks, false)
	local glowEffectTrack = GetKeyFrameBarEx(thirdPanel, directKeyframes["glowEffectTrack"], 0, ticks, false)

	local stopRecording = function()
		
		if isRecording then
			gAllowScreenEffects = false
			lastActiveCamera = nil //causes camera to reset
		//local x,y = go_HotboxFrame:GetPos()
		//go_HotboxFrame:SetPos(x,y - 100)
			//avi.End()
			gOverridePlayerMove	= false
			activeCameraTrack:SetState(MMDKeyFrameBar.STOPPED)
			blurEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
			colortintEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
			glowEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
			datastream.Send("DrawPlayerWeapon", {Value = true});
			gDisplayJunk = true
			isRecording = false
			recordButton:SetDisabled(false)
					   gFrameType = 0
		end
		tab[ "$pp_colour_addr" ] = 0
		tab[ "$pp_colour_addg" ] = 0 
		tab[ "$pp_colour_addb" ] = 0
		tab[ "$pp_colour_brightness" ] = 0 
		tab[ "$pp_colour_contrast" ] = 1
		tab[ "$pp_colour_colour" ] = 1
		tab[ "$pp_colour_mulr" ] = 0 
		tab[ "$pp_colour_mulg" ] = 0 
		tab[ "$pp_colour_mulb" ] = 0  

		blur["blend"] = 0
		blur["time"] = 0

		glow["multiply"] = 0
		glow["spread"] = 0
		glow["color"] = 0
	end

	secondPanel.Think = function()

		if input.IsKeyDown(KEY_SPACE) then
			isRecording = false
			gOverridePlayerMove	= false
			activeCameraTrack:SetState(MMDKeyFrameBar.STOPPED)
			blurEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
			colortintEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
			glowEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
			gDisplayJunk = true
			isRecording = false
			recordButton:SetDisabled(false)
			datastream.Send("DrawPlayerWeapon", {Value = true});
			CloseHotbox()
		end


		if isRecording then
			recordTime = recordTime + FrameTime()
			if recordTime + 10 > endPercent then
				stopRecording()
			end
			
		end

	end

	local start = 15

	recordButton.DoClick = function()
		
		if not isRecording then
			gAllowScreenEffects = true
			gOverridePlayerMove	= true
			activeCameraTrack:SetState(MMDKeyFrameBar.PLAYING)
			blurEffectTrack:SetState(MMDKeyFrameBar.PLAYING)
			colortintEffectTrack:SetState(MMDKeyFrameBar.PLAYING)
			glowEffectTrack:SetState(MMDKeyFrameBar.PLAYING)
			
			datastream.Send("DrawPlayerWeapon", {Value = false});
			
			blurEffectTrack.CurrentTime = startPercent * 0.01 * (blurEffectTrack.MaxTime - blurEffectTrack.MinTime) - 11
			colortintEffectTrack.CurrentTime = startPercent * 0.01 * (colortintEffectTrack.MaxTime - colortintEffectTrack.MinTime)- 11
			glowEffectTrack.CurrentTime = startPercent * 0.01 * (glowEffectTrack.MaxTime - glowEffectTrack.MinTime)- 11
			activeCameraTrack.CurrentTime = startPercent * 0.01 * (activeCameraTrack.MaxTime - activeCameraTrack.MinTime)- 11

			gDisplayJunk = false
			recordButton:SetDisabled(true)
			isRecording = true

			//HACK: we hide it so that it's compontns still update
			local x,y = go_HotboxFrame:GetPos()
//			go_HotboxFrame:SetPos(x,y + go_HotboxFrame:GetTall() - 0)
		end
		gOverridePlayerMove	= true
	end
recordButton:SetPos( secondPanel:GetWide() /2 - recordButton:GetWide()/ 2, 55) 
				 
	gOverridePlayerMove = false

	local lastActiveCamera = nil
	////////////
	//On Click Stop

	


	/////////////////////////////////////////


	
 	local viewLabel = GetTextLabelEx("Review", "Default", thirdPanel)	
	viewLabel:SetPos(20,5)

	GetSeperator(10,5 + controlLabel:GetTall() + (gi_Padding * 2), 160,0, secondPanel)	
			 

	activeCameraTrack:SetSize(panel:GetWide() - (firstPanel:GetWide() + 80+ secondPanel:GetWide()), 15)
	blurEffectTrack:SetSize(panel:GetWide() - (firstPanel:GetWide() + 80+ secondPanel:GetWide()), 15)
	colortintEffectTrack:SetSize(panel:GetWide() - (firstPanel:GetWide() + 80+ secondPanel:GetWide()), 15)
	glowEffectTrack:SetSize(panel:GetWide() - (firstPanel:GetWide() + 80+ secondPanel:GetWide()), 15)


	local wide = thirdPanel:GetWide() - 60

	local timeLabel = GetTextLabelEx("    [0: 0: 0.0000]    ", "Trebuchet22", secondPanel)	
	timeLabel:SizeToContents()
	timeLabel:SetPos(0.5 * (secondPanel:GetWide() - timeLabel:GetWide()),secondPanel:GetTall() - timeLabel:GetTall() - 4)
		
	local timelineTicks = GetPanelEx(gi_Padding + 25,gi_Padding + 20, thirdPanel:GetWide() - gi_Padding * 2,
	40, Color(0,255,0), thirdPanel) 
	timelineTicks.Paint = function()
	 	
		surface.SetDrawColor(110,130,74,255)
		surface.DrawLine( start + ( (endPercent ))-1, 25,start + ( (endPercent ))-1, 32)
		surface.DrawLine( start + ( (endPercent ))+1, 25,start + ( (endPercent ))+1, 32)

		surface.DrawLine( start , 25,start, 32)
		surface.DrawLine( start + 2, 25,start + 2, 32)
		surface.DrawLine( start + 1, 31,start + ((endPercent )), 31)
		
		surface.SetDrawColor(0,220,0,255)	
		surface.DrawLine( start + 1, 30,start + ((endPercent )), 30)
		surface.DrawLine( start + ( (endPercent )), 25,start + ( (endPercent )), 34)
		surface.DrawLine( start + 1, 25,start + 1, 34)
		
		
		

		local step = wide / (ticks)

		surface.SetDrawColor(255,255,255,255)	
		surface.DrawLine( start + step, 22,start + thirdPanel:GetWide() - 60 + 1, 22)

		for i=1,(ticks) do 
		
		if (i % 60) == 0 or i == 1 then
				
			surface.SetDrawColor( 233, 233, 233, 255 )
			surface.SetTextColor( 233, 233, 233, 255 )
			surface.SetTextPos( start + (i * step - 10), 0)
surface.SetFont("Default")
			surface.DrawText( string.format("%i:00", i / 60))

			surface.DrawLine( start + (i * step), 16,start + (i * step), 22) 
		else
			if i % 2 == 0 then
			surface.SetDrawColor( 180, 180, 180, 255 )
			surface.DrawLine( start + (i * step), 17,start + (i * step), 20) 
			end
		end

		end
	end

	local addButtonWidth = 40
 	
	local trackLabel = GetTextLabel("Camera", thirdPanel)	
	trackLabel:SetPos(5,63)
	trackLabel = GetTextLabel("M.Blur", thirdPanel)	
	trackLabel:SetPos(5,83)
	trackLabel = GetTextLabel("Color", thirdPanel)	
	trackLabel:SetPos(5,100)
	trackLabel = GetTextLabel("Glow", thirdPanel)	
	trackLabel:SetPos(5,119)

	activeCameraTrack:SetPos(start * 2.75,60)
	blurEffectTrack:SetPos(start * 2.75,82)
	colortintEffectTrack:SetPos(start * 2.75,100)
	glowEffectTrack:SetPos(start * 2.75,118)

	activeCameraTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		activeCameraTrack:SetState(MMDKeyFrameBar.STOPPED)
	end
	blurEffectTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		blurEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
	end
	colortintEffectTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		colortintEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
	end
	glowEffectTrack.OnSampleFinished = function()
		gOverridePlayerMove = false
		glowEffectTrack:SetState(MMDKeyFrameBar.STOPPED)
	end


	activeCameraTrack.OnSample = function(frac, keyDataA, keyDataB)

		local entityA = keyDataA.Data.CameraEntity

		if entityA and entityA:IsValid() then
			
			gViewData = entityA:PlaybackData()
			if entityA != lastActiveCamera then
				gFrameType = GetRootNode(entityA):GetNWInt(ANICAM_FRAMETYPE)
				//causes playback to start from begininng
				entityA.onPlaybackNode = nil	
				entityA.onPlaybackNodeTime = 0
				lastActiveCamera =entityA
			end
		end
	end
	
	blurEffectTrack.OnSample = function(frac, keyDataA, keyDataB)
	
		local minPerc = keyDataA.Percentage
		local minData = keyDataA.Data
		local maxPerc = keyDataB.Percentage
		local maxData = keyDataB.Data

		local percDelta = maxPerc - minPerc
		if percDelta > 0.0 then
			local lerp = (frac - minPerc) / percDelta
			local invLerp = 1.0 - lerp

			blur[ "blend" ] = (invLerp * minData.BlendAmount) + (lerp * maxData.BlendAmount)
			blur[ "time" ] = (invLerp * minData.MotionBlurTime) + (lerp * maxData.MotionBlurTime)
		else
			blur[ "blend" ] = minData.BlendAmount
			blur[ "time" ] = minData.MotionBlurTime
		end
	end

	colortintEffectTrack.OnSample = function(frac, keyDataA, keyDataB)
	
		local minPerc = keyDataA.Percentage
		local minData = keyDataA.Data
		local maxPerc = keyDataB.Percentage
		local maxData = keyDataB.Data

		local percDelta = maxPerc - minPerc
		if percDelta > 0.0 then
		
			local lerp = (frac - minPerc) / percDelta
			local invLerp = 1.0 - lerp

			tab[ "$pp_colour_addr" ] = 0
			tab[ "$pp_colour_addg" ] = 0 
			tab[ "$pp_colour_addb" ] = 0 
			tab[ "$pp_colour_brightness" ] = (invLerp * minData.Brightness) + (lerp * maxData.Brightness)
			tab[ "$pp_colour_contrast" ] = (invLerp * minData.Contrast) + (lerp * maxData.Contrast)
			tab[ "$pp_colour_colour" ] = 1 
			tab[ "$pp_colour_mulr" ] = (invLerp * minData.Red) + (lerp * maxData.Red)
			tab[ "$pp_colour_mulg" ] = (invLerp * minData.Green) + (lerp * maxData.Green)
			tab[ "$pp_colour_mulb" ] = (invLerp * minData.Blue) + (lerp * maxData.Blue)

		else
			
			tab[ "$pp_colour_addr" ] = 0
			tab[ "$pp_colour_addg" ] = 0 
			tab[ "$pp_colour_addb" ] = 0 
			tab[ "$pp_colour_brightness" ] = minData.Brightness
			tab[ "$pp_colour_contrast" ] = minData.Contrast
			tab[ "$pp_colour_colour" ] = 1 
			tab[ "$pp_colour_mulr" ] = minData.Red
			tab[ "$pp_colour_mulg" ] = minData.Green
			tab[ "$pp_colour_mulb" ] = minData.Blue

		end
	end

	colortintEffectTrack.OnAddKeyframe = function() 
		local newModel = {ParentKey = "colortintEffectTrack", Percentage = 0.0, Panel = nil, Data = {Brightness = 0.5, Contrast = 0.5, Red = 0.1, Green = 0.1, Blue = 0.1}}
		table.insert(directKeyframes["colortintEffectTrack"], 1, newModel)
		return newModel
	end
	colortintEffectTrack.OnLeftClickKeyframe = function(keyframeModel) 
		if OnClickKeyframe(keyframeModel) then
			SwitchKeyframePanel(ColorTintKeyframePanel(thirdPanel, keyframeModel),thirdPanel) 
		end
	 end
	
	glowEffectTrack.OnSample = function(frac, keyDataA, keyDataB)
	
		local minPerc = keyDataA.Percentage
		local minData = keyDataA.Data
		local maxPerc = keyDataB.Percentage
		local maxData = keyDataB.Data

		local percDelta = maxPerc - minPerc
		if percDelta > 0.0 then
			local lerp = (frac - minPerc) / percDelta
			local invLerp = 1.0 - lerp

			glow[ "multiply" ] = (invLerp * minData.Multiply) + (lerp * maxData.Multiply)
			glow[ "spread" ] = (invLerp * minData.Spread) + (lerp * maxData.Spread)
			glow[ "color" ] = (invLerp * minData.Color) + (lerp * maxData.Color)
		else
			glow[ "multiply" ] = minData.Multiply
			glow[ "spread" ] = minData.Spread
			glow[ "color" ] = minData.Color
		end
	end

end






function SwitchHotboxPanel(panel)
	if go_HotboxCurrentPanel != nil and go_HotboxCurrentPanel != panel then
		go_HotboxCurrentPanel:Remove()
		go_HotboxCurrentPanel = panel
	end
end

/********************************************
 * Displays the 'Hotbox' with respect to the 
 * server's mode
 ********************************************/
function ProducerInterface( messageData )
	if messageData and messageData.traceType then
		if go_HotboxFrame == nil then
			go_HotboxFrame = vgui.Create( "DFrame" )
			go_HotboxFrame:MakePopup()
		else
			go_HotboxFrame:MakePopup()
		end
		
		go_HotboxFrame:SetPos( -1 , ScrH() - gi_HotboxHeight) 
		go_HotboxFrame:SetSize( ScrW() + 2, gi_HotboxHeight) 
		go_HotboxFrame:SetVisible( true ) 
		go_HotboxFrame:SetDraggable( false ) 
		go_HotboxFrame:SetScreenLock( true ) 
		go_HotboxFrame:ShowCloseButton( false ) 
		go_HotboxFrame:SetDeleteOnClose( false ) 


		local pinButton = GetImageButton("gui/MMMatCrossButton", gi_PinButtonWidth, gi_HotboxVerticalAnchor,CloseHotbox , go_HotboxFrame)
		pinButton:SetPos( ScrW() - gi_PinButtonWidth, 0 )

		local constructOnClick = function ()
			SwitchHotboxPanel(CreatePanel(go_HotboxFrame))
		end
		local constructButton = GetButton("Create", constructOnClick, go_HotboxFrame)
		constructButton:SetPos( 1, 0 )
		constructButton:SetSize( gi_ConstructButtonWidth, gi_HotboxVerticalAnchor )
		
		local directOnClick = function ()
			SwitchHotboxPanel(DirectPanel(go_HotboxFrame))
		end

		local directButton = GetButton("Direct", directOnClick, go_HotboxFrame)
		directButton:SetPos( 1 + gi_ConstructButtonWidth, 0 )
		directButton:SetSize( gi_DirectButtonWidth, gi_HotboxVerticalAnchor )	
		
		local produceOnClick = function ()
			SwitchHotboxPanel(ProducePanel(go_HotboxFrame))
		end

		local produceButton = GetButton("Produce", produceOnClick, go_HotboxFrame)
		produceButton:SetPos( 1 + gi_DirectButtonWidth + gi_ConstructButtonWidth, 0 )
		produceButton:SetSize( gi_ProduceButtonWidth, gi_HotboxVerticalAnchor )
		
		/***********************************************				  
		 * Build the Producer panel
		 ***********************************************/
		if messageData.traceType == "Default" then

			SwitchHotboxPanel(CreatePanel(go_HotboxFrame))

		///***********************************************				  
		// * Build the panel for the actor sequence node entity
		// ***********************************************/
		elseif messageData.traceType == "ActorSequenceNode" then

			local selectedEntity = ents.GetByIndex(messageData.entIndex)

			/*******************************************
			 * Initalize main controls
			 *******************************************/
			local deleteTrackButton = GetButton("Delete Entire Sequence", nil, go_HotboxFrame)
			deleteTrackButton:SetPos( 1, 0 )
			deleteTrackButton:SetSize( gi_ConstructButtonWidth, gi_HotboxVerticalAnchor )
			deleteTrackButton:SetSelected(selectedEntity:GetNWBool("IsFreeze"))
			deleteTrackButton.DoClick = function ()
				local eventData		= {}
				eventData.entIndex	= selectedEntity:EntIndex()
				datastream.Send("DeleteCameraTrack", eventData);
			end
			
			local deleteButton = GetButton("Delete Waypoint", nil, go_HotboxFrame)
			deleteButton:SetPos( 1 + gi_ConstructButtonWidth, 0 )
			deleteButton:SetSize( gi_DirectButtonWidth, gi_HotboxVerticalAnchor )	
			deleteButton.DoClick = function ()
				local eventData		= {}
				eventData.entIndex	= selectedEntity:EntIndex()
				datastream.Send("DeleteCamera", eventData);
			end

			local spacer = GetButton("", nil, go_HotboxFrame)
			spacer:SetPos( 1 + gi_DirectButtonWidth + gi_ConstructButtonWidth, 0 )
			spacer:SetSize( gi_ProduceButtonWidth, gi_HotboxVerticalAnchor )
			spacer:SetDisabled(true)
				
			SwitchHotboxPanel(ActorSequencePanel(go_HotboxFrame, selectedEntity))

		/***********************************************				  
		 * Build the panel for the animated camera entity
		 ***********************************************/
		elseif messageData.traceType == "AnimatedCameraNode" then
			
			local selectedEntity = ents.GetByIndex(messageData.entIndex)

			/*******************************************
			 * Initalize main controls
			 *******************************************/
			local deleteTrackButton = GetButton("Delete Track", nil, go_HotboxFrame)
			deleteTrackButton:SetPos( 1, 0 )
			deleteTrackButton:SetSize( gi_ConstructButtonWidth, gi_HotboxVerticalAnchor )
			deleteTrackButton:SetSelected(selectedEntity:GetNWBool("IsFreeze"))
			deleteTrackButton.DoClick = function ()
				local eventData		= {}
				eventData.entIndex	= selectedEntity:EntIndex()
				datastream.Send("DeleteCameraTrack", eventData);
			end
			
			local deleteButton = GetButton("Delete Camera", nil, go_HotboxFrame)
			deleteButton:SetPos( 1 + gi_ConstructButtonWidth, 0 )
			deleteButton:SetSize( gi_DirectButtonWidth, gi_HotboxVerticalAnchor )	
			deleteButton.DoClick = function ()
				local eventData		= {}
				eventData.entIndex	= selectedEntity:EntIndex()
				datastream.Send("DeleteCamera", eventData);
			end

			local spacer = GetButton("", nil, go_HotboxFrame)
			spacer:SetPos( 1 + gi_DirectButtonWidth + gi_ConstructButtonWidth, 0 )
			spacer:SetSize( gi_ProduceButtonWidth, gi_HotboxVerticalAnchor )
			spacer:SetDisabled(true)
				
			SwitchHotboxPanel(AnimatedCameraPanel(go_HotboxFrame, selectedEntity))


		/***********************************************				  
		 * Build the panel for the static camera entity
		 ***********************************************/
		elseif messageData.traceType == "StaticCameraNode" then


		/***********************************************				  
		 * Build the panel for the selected entity
		 ***********************************************/
		elseif messageData.traceType == "PhysicsEntity" then

	 		local selectedEntity = ents.GetByIndex(messageData.entIndex)
			SwitchHotboxPanel(PropPanel(go_HotboxFrame, selectedEntity))
		end
	end

end

datastream.Hook("ShowUIPhysEnt", "ShowUIPhysEnt", ProducerInterface,nil);


end	//CLIENT
